﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using DP.Web.UI;
using CRM.BLL;
using CRM.Entity;
using DP.Common;

namespace CRM.Web.Pop
{
    public partial class ProcessingUserSelected : Common.PageBase
    {
        private UserLoginInfo _userLoginInfo;
        T_USER_BLL userBll = new T_USER_BLL();

        protected override void OnInit(EventArgs e)
        {
            base.VerificationRight = false;
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            UserLoginInfo.CheckLoginInfo();
            _userLoginInfo = UserLoginInfo.GetLoginInfo();
            if (!IsPostBack)
            {

                gvDataBinding();
            }
        }

        /// <summary>
        /// 生成查询条件字符串
        /// </summary>
        private StringBuilder GenerationSQLString()
        {
            StringBuilder sql = new StringBuilder();
            if (!String.IsNullOrEmpty(Request.QueryString["USERID"]))
            {
                sql.Append(" and USERID like '%" + Request.QueryString["USERID"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["USERNAME"]))
            {
                sql.Append(" and USERNAME like '%" + Request.QueryString["USERNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LOGINNAME"]))
            {
                sql.Append(" and LOGINNAME like '%" + Request.QueryString["LOGINNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PASSWORD"]))
            {
                sql.Append(" and PASSWORD like '%" + Request.QueryString["PASSWORD"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["GENDER"]))
            {
                sql.Append(" and GENDER like '%" + Request.QueryString["GENDER"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DEPARTMENTID"]))
            {
                sql.Append(" and DEPARTMENTID = '" + Request.QueryString["DEPARTMENTID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DEPARTMENTNAME"]))
            {
                sql.Append(" and DEPARTMENTNAME like '%" + Request.QueryString["DEPARTMENTNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ROLEID"]))
            {
                sql.Append(" and ROLEID = '" + Request.QueryString["ROLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ROLENAME"]))
            {
                sql.Append(" and ROLENAME like '%" + Request.QueryString["ROLENAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LOGINCOUNT"]))
            {
                sql.Append(" and LOGINCOUNT like '%" + Request.QueryString["LOGINCOUNT"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTLOGINTIMEBegin"]))
            {
                sql.Append(" and " + userBll._dal.GetBeginDateString("LASTLOGINTIME", Request.QueryString["LASTLOGINTIMEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTLOGINTIMEEnd"]))
            {
                sql.Append(" and " + userBll._dal.GetEndDateString("LASTLOGINTIME", Request.QueryString["LASTLOGINTIMEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["STATUS"]))
            {
                sql.Append(" and STATUS like '%" + Request.QueryString["STATUS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BIRTHDATEBegin"]))
            {
                sql.Append(" and BIRTHDATE >= '" + Request.QueryString["BIRTHDATEBegin"].Trim() + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BIRTHDATEEnd"]))
            {
                sql.Append(" and BIRTHDATE < '" + Request.QueryString["BIRTHDATEEnd"].Trim() + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SEATID"]))
            {
                sql.Append(" and SEATID = '" + Request.QueryString["SEATID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PWDLASTMODIFYTIMEBegin"]))
            {
                sql.Append(" and " + userBll._dal.GetBeginDateString("PWDLASTMODIFYTIME", Request.QueryString["PWDLASTMODIFYTIMEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PWDLASTMODIFYTIMEEnd"]))
            {
                sql.Append(" and " + userBll._dal.GetEndDateString("PWDLASTMODIFYTIME", Request.QueryString["PWDLASTMODIFYTIMEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LOCKED"]))
            {
                sql.Append(" and LOCKED like '%" + Request.QueryString["LOCKED"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PWDRESET"]))
            {
                sql.Append(" and PWDRESET like '%" + Request.QueryString["PWDRESET"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MOBILEPHONE"]))
            {
                sql.Append(" and MOBILEPHONE like '%" + Request.QueryString["MOBILEPHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["HOMEPHONE"]))
            {
                sql.Append(" and HOMEPHONE like '%" + Request.QueryString["HOMEPHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["EMAIL"]))
            {
                sql.Append(" and EMAIL like '%" + Request.QueryString["EMAIL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["URL"]))
            {
                sql.Append(" and URL like '%" + Request.QueryString["URL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["QQ"]))
            {
                sql.Append(" and QQ like '%" + Request.QueryString["QQ"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MSN"]))
            {
                sql.Append(" and MSN like '%" + Request.QueryString["MSN"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["EXT"]))
            {
                sql.Append(" and EXT like '%" + Request.QueryString["EXT"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEBegin"]))
            {
                sql.Append(" and " + userBll._dal.GetBeginDateString("CREATETIME", Request.QueryString["CREATETIMEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEEnd"]))
            {
                sql.Append(" and " + userBll._dal.GetEndDateString("CREATETIME", Request.QueryString["CREATETIMEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLEID"]))
            {
                sql.Append(" and CREATEPEOPLEID = '" + Request.QueryString["CREATEPEOPLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLE"]))
            {
                sql.Append(" and CREATEPEOPLE like '%" + Request.QueryString["CREATEPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYPEOPLEID"]))
            {
                sql.Append(" and LASTMODIFYPEOPLEID = '" + Request.QueryString["LASTMODIFYPEOPLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYPEOPLE"]))
            {
                sql.Append(" and LASTMODIFYPEOPLE like '%" + Request.QueryString["LASTMODIFYPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYTIMEBegin"]))
            {
                sql.Append(" and " + userBll._dal.GetBeginDateString("LASTMODIFYTIME", Request.QueryString["LASTMODIFYTIMEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYTIMEEnd"]))
            {
                sql.Append(" and " + userBll._dal.GetEndDateString("LASTMODIFYTIME", Request.QueryString["LASTMODIFYTIMEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["REMARK"]))
            {
                sql.Append(" and REMARK like '%" + Request.QueryString["REMARK"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["USERTYPE"]))
            {
                sql.Append(" and USERTYPE = '" + Request.QueryString["USERTYPE"] + "'");
            }
            sql.Append(" and (ISSYSTEMADMIN <> '1' or ISSYSTEMADMIN is null) ");
            if (!UserLoginInfo.IsAdministrator())
            {
                sql.Append(" and BUSINESSDIVISIONCODE = '" + _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim() + "'");
            }
            return sql;

        }

        /// <summary>
        /// 页面数据绑定
        /// </summary>
        private void gvDataBinding()
        {
            #region 查询条件字符串
            StringBuilder sql = GenerationSQLString();
            #endregion

            List<T_USER_Entity> list;

            //获取总的记录条数
            //gvList.RecordCount = (int)userBll.GetCount(sql.ToString());

            list = userBll.GetList(sql.ToString(), gvList.SortConditions);

            gvList.DataSource = list;
            gvList.DataKeyNames = new string[] { "USERID", "MOBILEPHONE", "UserName", "LoginName" };
            gvList.DataBind();

        }

        protected void btnSelected_Click(object sender, EventArgs e)
        {
            StringBuilder sbUserLogin = new StringBuilder();
            StringBuilder sbUserName = new StringBuilder();
            var list = GetSelected(gvList);
            foreach (var userEntity in list)
            {
                if(sbUserName.Length != 0)
                {
                    sbUserName.Append(";");
                }
                sbUserName.Append(userEntity.MOBILEPHONE).Append("*").Append(userEntity.USERNAME);
                if(sbUserLogin.Length != 0)
                {
                    sbUserLogin.Append(";");
                }
                sbUserLogin.Append(userEntity.USERID.ToString());
            }
            PageHelper.ExecuteJS(String.Format("window.opener.SetProcessingUser('{0}','{1}'); closeSelf(); ", sbUserLogin.ToString(), sbUserName.ToString()));
        }

        private List<T_USER_Entity> GetSelected(GridView gv)
        {
            List<T_USER_Entity> list = new List<T_USER_Entity>();
            if (gv == null) return list;
            T_USER_Entity entity;
            CheckBox cb;
            foreach (GridViewRow gvr in gv.Rows)
            {
                cb = gvr.FindControl("cbItem") as CheckBox;
                if (cb == null) continue;
                if (!cb.Checked) continue;
                entity = new T_USER_Entity();
                entity.USERID = StringHelper.ToInt(gv.DataKeys[gvr.RowIndex].Values["USERID"].ToString(), 0);
                entity.USERNAME = gv.DataKeys[gvr.RowIndex].Values["UserName"].ToString();
                entity.LOGINNAME = gv.DataKeys[gvr.RowIndex].Values["LoginName"].ToString();
                entity.MOBILEPHONE = gv.DataKeys[gvr.RowIndex].Values["MOBILEPHONE"].ToString();
                list.Add(entity);
            }
            return list;
        }
    }
}
